package godraines.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class CoupleMaker extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	
	private static String MetalLook = "javax.swing.plaf.metal.MetalLookAndFeel";
	private JTextField uid;
	private JPasswordField pwd;
	private JTable table;
	private JTable table_1;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame.setDefaultLookAndFeelDecorated(true);
					JDialog.setDefaultLookAndFeelDecorated(true);
					UIManager.setLookAndFeel( MetalLook );
					CoupleMaker frame = new CoupleMaker();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CoupleMaker() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(CoupleMaker.class.getResource("/images/icon/icon.png")));
		setTitle("연인만들기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(800, 100));
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_5.setBackground(new Color(255, 153, 0, 70));
		panel_5.setPreferredSize(new Dimension(200, 100));
		panel.add(panel_5, BorderLayout.EAST);
		
		uid = new JTextField();
		uid.setToolTipText("아이디를 입력하세요");
		uid.setColumns(10);
		
		pwd = new JPasswordField();
		pwd.setToolTipText("비밀번호를 입력하세요");
		
		final JButton loginBtn = new JButton("로그인");
		final JButton joinBtn = new JButton("회원가입");
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ( loginBtn.getText().equals("로그인") ){
					if ( uid.getText().equals("") && Arrays.equals("".toCharArray(), pwd.getPassword() ) ) {		// 배열이 같은지 검사한다. 검사할 데이터의 배열화 한 값과 받은 값
						JOptionPane.showMessageDialog(null, "로그인 성공");														// getPassword가 배열값이기 때문에 배열로 검사해야함
						loginBtn.setText("로그아웃");
						joinBtn.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(null, "로그인 실패");
					}
				} else if (loginBtn.getText().equals("로그아웃") ) {
					JOptionPane.showMessageDialog(null, "로그아웃 성공");
					loginBtn.setText("로그인");
					joinBtn.setVisible(true);
				}
			}
		});
		
		//JButton joinBtn = new JButton("회원가입");						// loginBtn의 상태에 따라 joinBtn을 보여주고 가려주는 작업을 하기 위해 위에서 생성 함
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_5.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_panel_5.createSequentialGroup()
							.addComponent(loginBtn)
							.addGap(18)
							.addComponent(joinBtn))
						.addGroup(gl_panel_5.createParallelGroup(Alignment.TRAILING)
							.addComponent(uid, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
							.addComponent(pwd)))
					.addContainerGap(20, Short.MAX_VALUE))
		);
		gl_panel_5.setVerticalGroup(
			gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_5.createSequentialGroup()
					.addContainerGap()
					.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_5.createParallelGroup(Alignment.BASELINE)
						.addComponent(joinBtn)
						.addComponent(loginBtn))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_5.setLayout(gl_panel_5);
		
		JPanel panel_4 = new JPanel();
		panel.add(panel_4, BorderLayout.WEST);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(CoupleMaker.class.getResource("/images/background/top.jpg")));
		panel_4.add(lblNewLabel);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_1, null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(500, 200));
		panel_1.add(scrollPane);
		
		// 성적 데이터 생성
		ArrayList<J23SungJukVO> sjlist = new ArrayList<>();
		sjlist.add( new J23SungJukVO("미미", 98, 97, 94));
		sjlist.add( new J23SungJukVO("미화", 87, 89, 84));
		sjlist.add( new J23SungJukVO("미진", 79, 76, 73));
		sjlist.add( new J23SungJukVO("미영", 66, 69, 67));
		sjlist.add( new J23SungJukVO("미희", 57, 53, 54));
		
		// JTable에 배열 데이터를 저장하기 위해 defaultTableModel 클래스를 작성
		DefaultTableModel model = new DefaultTableModel();
		
		// 테이블의 제목줄에 사용할 항목 지정
		model.setColumnIdentifiers( new String[] {"이름", "국어", "영어", "수학"} );
		
		// 테이블의 본문행을 작성
		for ( J23SungJukVO sj : sjlist ) {
			model.addRow( new String[] { sj.getName(), sj.getKor()+"", sj.getEng()+"", sj.getMat()+"" });
		}
		
		table = new JTable( model );																	// 테이블 생성시에 위에서 작성한 테이블의 내용들을 입력 받아서 만든다.
		// table.setModel( model );																		// 생성자에 안만들고 이렇게 해도 되는데 라인이 한줄 더 추가 되므로 생성자에서 만들자
		table.setPreferredSize(new Dimension(500, 200));
		scrollPane.setViewportView(table);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_2, null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setPreferredSize(new Dimension(500, 200));
		panel_2.add(scrollPane_1);
		
		table_1 = new JTable();
		table_1.setPreferredSize(new Dimension(500, 200));
		scrollPane_1.setViewportView(table_1);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_3, null);
		
		JButton button = new JButton("New button");
		panel_3.add(button);
		setLocationRelativeTo(null);
	}
}
